package com.bensoft.minesweeper.gui.minesweeperframe;

import com.bensoft.minesweeper.ApplicationController;
import com.bensoft.minesweeper.GameManager;
import com.bensoft.minesweeper.gui.AboutDialog;
import com.bensoft.minesweeper.gui.CustomGameDialog;
import com.bensoft.minesweeper.gui.GUILoader;
import com.bensoft.minesweeper.gui.HighScoreDialog;
import com.bensoft.minesweeper.gui.preferences.ApplicationPreferences;
import com.bensoft.minesweeper.resources.FileResources;
import com.bensoft.minesweeper.resources.strings.StringResources;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

/**
 *
 * @author Ben Wolsieffer
 */
public class MenuBar extends JMenuBar implements GUILoader {

    private GameMenu gameMenu;
    private HelpMenu helpMenu;

    public MenuBar() {
        init();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    @Override
    public void initializeComponents() {
        gameMenu = new GameMenu();
        helpMenu = new HelpMenu();
    }

    @Override
    public void setLayouts() {
    }

    @Override
    public void addComponents() {
        add(gameMenu);
        add(helpMenu);
    }

    @Override
    public void addComponentListeners() {
    }

    @Override
    public void otherInit() {
    }

    private class GameMenu extends JMenu implements GUILoader {

        private JMenuItem newGameItem, highScoreItem, exitItem;
        private JRadioButtonMenuItem beginnerMenuItem,
                intermediateMenuItem,
                advancedMenuItem,
                customMenuItem;
        private JCheckBoxMenuItem marksItem, soundsItem;
        private JMenu themesMenu;
        private ButtonGroup gameTypeGroup, themeGroup;
        private CustomGameDialog customGameDialog;
        private HighScoreDialog highScoreDialog;

        public GameMenu() {
            init();
        }

        private void init() {
            initializeComponents();
            setLayouts();
            addComponents();
            addComponentListeners();
            otherInit();
        }

        @Override
        public void initializeComponents() {
            newGameItem = new JMenuItem(StringResources.getString("gui.menu.game.newgame"));
            gameTypeGroup = new ButtonGroup();
            beginnerMenuItem = new JRadioButtonMenuItem(StringResources.getString("gui.menu.game.beginner"));
            intermediateMenuItem = new JRadioButtonMenuItem(StringResources.getString("gui.menu.game.intermediate"));
            advancedMenuItem = new JRadioButtonMenuItem(StringResources.getString("gui.menu.game.advanced"));
            customMenuItem = new JRadioButtonMenuItem(StringResources.getString("gui.menu.game.custom"));
            customGameDialog = new CustomGameDialog();
            marksItem = new JCheckBoxMenuItem(StringResources.getString("gui.menu.game.marks"), ApplicationPreferences.areQuestionMarksEnabled());
            soundsItem = new JCheckBoxMenuItem(StringResources.getString("gui.menu.game.sounds"), true);
            highScoreItem = new JMenuItem(StringResources.getString("gui.menu.game.highscores"));
            highScoreDialog = new HighScoreDialog();
            themeGroup = new ButtonGroup();
            themesMenu = new JMenu(StringResources.getString("gui.menu.game.themes"));
            exitItem = new JMenuItem(StringResources.getString("gui.menu.game.exit"));
        }

        @Override
        public void setLayouts() {
        }

        @Override
        public void addComponents() {
            add(newGameItem);
            addSeparator();
            gameTypeGroup.add(beginnerMenuItem);
            gameTypeGroup.add(intermediateMenuItem);
            gameTypeGroup.add(advancedMenuItem);
            gameTypeGroup.add(customMenuItem);
            add(beginnerMenuItem);
            add(intermediateMenuItem);
            add(advancedMenuItem);
            add(customMenuItem);
            addSeparator();
            add(marksItem);
            add(soundsItem);
            addSeparator();
            add(highScoreItem);
            addSeparator();
            String currentTheme = FileResources.getCurrentTheme();
            for (final Object theme : FileResources.getThemes()) {
                JRadioButtonMenuItem m = new JRadioButtonMenuItem((String)theme);
                
                m.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        FileResources.setTheme((String)theme);
                    }
                });
                themeGroup.add(m);
                if(currentTheme.equals(theme)) {
                    m.setSelected(true);
                }
                themesMenu.add(m);
            }
            
            add(themesMenu);
            addSeparator();
            add(exitItem);
        }

        @Override
        public void addComponentListeners() {
            newGameItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ApplicationController.getGameManager().resetGame();
                }
            });
            beginnerMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    GameManager gm = ApplicationController.getGameManager();
                    gm.setGridSize(9, 9, 10);
                    gm.resetGame();
                }
            });
            intermediateMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    GameManager gm = ApplicationController.getGameManager();
                    gm.setGridSize(16, 16, 40);
                    gm.resetGame();
                }
            });
            advancedMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    GameManager gm = ApplicationController.getGameManager();
                    gm.setGridSize(30, 16, 99);
                    gm.resetGame();
                }
            });
            customMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    customGameDialog.setLocationRelativeTo(ApplicationController.getMinesweeperFrame());
                    customGameDialog.setVisible(true);
                }
            });
            marksItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ApplicationPreferences.setQuestionMarks(marksItem.isSelected());
                }
            });
            soundsItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ApplicationPreferences.setSoundsOn(soundsItem.isSelected());
                }
            });
            highScoreItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    highScoreDialog.setVisible(true);
                    //ApplicationController.getGameManager().gameWon();
                }
            });
            exitItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }

        @Override
        public void otherInit() {
            if (ApplicationController.getGameManager().isBeginnerMode()) {
                gameTypeGroup.setSelected(beginnerMenuItem.getModel(), true);
            } else if (ApplicationController.getGameManager().isIntermediateMode()) {
                gameTypeGroup.setSelected(intermediateMenuItem.getModel(), true);
            } else if (ApplicationController.getGameManager().isAdvancedMode()) {
                gameTypeGroup.setSelected(advancedMenuItem.getModel(), true);
            } else {
                gameTypeGroup.setSelected(customMenuItem.getModel(), true);
            }
            setText(StringResources.getString("gui.menu.game.title"));
            newGameItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
        }
    }

    private class HelpMenu extends JMenu implements GUILoader {

        private JMenuItem aboutItem;
        private AboutDialog aboutDialog;

        public HelpMenu() {
            init();
        }

        private void init() {
            initializeComponents();
            setLayouts();
            addComponents();
            addComponentListeners();
            otherInit();
        }

        @Override
        public void initializeComponents() {
            aboutItem = new JMenuItem(StringResources.getString("gui.menu.help.about"));
            aboutDialog = new AboutDialog();
        }

        @Override
        public void setLayouts() {
        }

        @Override
        public void addComponents() {
            add(aboutItem);
        }

        @Override
        public void addComponentListeners() {
            aboutItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    aboutDialog.setVisible(true);
                }
            });
        }

        @Override
        public void otherInit() {
            setText(StringResources.getString("gui.menu.help.title"));
        }
    }
}
